package com.greenhou.sqlSession;

import com.greenhou.config.XmlConfigBuilder;
import com.greenhou.pojo.Configuration;
import com.greenhou.sqlSession.defaults.DefaultSqlSessionFactory;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

/**
 * @ClassName SqlSessionFactoryBuilder
 * @Description 第一：解析xml并将解析的呈结构的数据存到容器中。
 *              第二：创建SqlSessionFactory对象，生产SqlSession 会话对象
 * @Author YR
 * @Date 2020/6/8 22:05
 * @Version 1.0
 */
public class SqlSessionFactoryBuilder {
    /*
     *该方法用于解析xml并构建一个SqlSessionFactory
     */
    public SqlSessionFactory build(InputStream in) throws DocumentException, PropertyVetoException {
        //第一：使用dom4j解析配置文件，将解析出来的内容封装到Configuration
        XmlConfigBuilder xmlConfigBuilder = new XmlConfigBuilder();
        Configuration configuration = xmlConfigBuilder.parseConfig(in);
        //第二：创建SqlSessionFactory对象
        DefaultSqlSessionFactory defaultSqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        return defaultSqlSessionFactory;
    }
}
